package com.mdd.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IMemberService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.member.MemberCreateValidate;
import com.mdd.admin.validate.member.MemberExamineValicate;
import com.mdd.admin.validate.member.MemberSearchValidate;
import com.mdd.admin.vo.member.MemberListsVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.member.Member;
import com.mdd.common.mapper.member.MemberMapper;
import com.mdd.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class MemberServiceImpl implements IMemberService {

    @Resource
    MemberMapper memberMapper;

    @Override
    public PageResult<MemberListsVo> list(PageValidate pageValidate, MemberSearchValidate searchValidate) {
        Integer pageNo   = pageValidate.getPage_no();
        Integer pageSize = pageValidate.getPage_size();
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ObjectUtil.isNotEmpty(searchValidate.getStatus()),Member::getStatus,searchValidate.getStatus())
                .and(StringUtils.isNotEmpty(searchValidate.getKeyWork()),a->a.like(Member::getName,searchValidate.getKeyWork())
                        .or().like(Member::getStudentNum,searchValidate.getKeyWork())
                        .or().like(Member::getContact,searchValidate.getKeyWork())
                        .or().like(Member::getCollege,searchValidate.getKeyWork())
                );
        queryWrapper.lambda().orderByAsc(Member::getId);
        Page<Member> iPage = memberMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
        List<MemberListsVo> list = new LinkedList<>();
        for(Member item : iPage.getRecords()) {
            MemberListsVo vo = new MemberListsVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public Member detail(Integer id, Integer competitionId, Integer schedule) {
        return memberMapper.selectById(id);
    }

    @Override
    public void add(MemberCreateValidate createValidate) {
        Member member = new Member();
        BeanUtils.copyProperties(createValidate,member);
        memberMapper.insert(member);
    }

    @Override
    public void edit(MemberCreateValidate updateValidate) {
        Member member = new Member();
        BeanUtils.copyProperties(updateValidate,member);
        memberMapper.updateById(member);
    }

    @Override
    public void memberExamine(MemberExamineValicate valicate) {
        Member model = memberMapper.selectOne(
                new QueryWrapper<Member>()
                        .eq("id", valicate.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setStatus(valicate.getStatus());
        memberMapper.updateById(model);
    }
}
